import { observable, flow, action, configure } from 'mobx';
import _ from 'lodash';
import { Actions } from 'react-native-router-flux';
import {
  getKpi,
  getTasks,
  createTask,
  doneTask,
  cancelTask,
  updateTask,
  removeTask,
  getTask,
} from '../services';
import { getCustomers } from '../../customer/services';

configure({ enforceActions: true });

class Home {
  @observable
  kpi = [
    /* {
      count: 12,
      name: "客户数",
      percentageRanking: 69
    },
    {
      count: 38,
      name: "拜访量",
      percentageRanking: 78
    },
    {
      count: 29,
      name: "录单量",
      percentageRanking: 59
    }, */
  ];
  @observable missions = {
    /* tasks: [
      {
        creationTime: "2018-09-01T05:53:40.137",
        customerBirthday: "1999/1/1 0:00:00",
        customerGender: "1",
        customerId: 10053,
        customerMobile: "13211112222",
        customerName: "达芬奇",
        description: "拜访客户 达芬奇",
        scheduledTime: "2018-09-02T05:53:00",
        state: 1,
        taskId: 10016,
        title: "拜访客户 达芬奇",
        type: 3
      },
      {
        creationTime: "2018-09-01T05:53:40.137",
        customerBirthday: "1999/1/1 0:00:00",
        customerGender: "1",
        customerId: 10053,
        customerMobile: "13211112222",
        customerName: "达芬奇",
        description: "2018-09-03 拉斐尔要做生日，记得送祝福",
        scheduledTime: "2018-09-03T05:53:00",
        state: 1,
        taskId: 10017,
        title: "拉斐尔36岁生日",
        type: 1
      },
      {
        creationTime: "2018-09-01T05:53:40.137",
        customerBirthday: "1999/1/1 0:00:00",
        customerGender: "1",
        customerId: 10053,
        customerMobile: "13211112222",
        customerName: "米开朗基罗的",
        description: '天安人寿，健康源(尊享)',
        scheduledTime: "2018-09-04T05:53:00",
        state: 1,
        taskId: 10019,
        title: "米开朗基罗的保单续期",
        type: 2
      },
      {
        creationTime: "2018-09-01T05:53:40.137",
        customerBirthday: "1999/1/1 0:00:00",
        customerGender: "1",
        customerId: 10053,
        customerMobile: "13211112222",
        customerName: "毕沙罗",
        description: "陪同 塞尚去拜访毕沙罗，刚生了儿子",
        scheduledTime: "2018-09-02T05:53:00",
        state: 1,
        taskId: 10020,
        title: "陪访 毕沙罗",
        type: 3
      },
      {
        creationTime: "2018-09-01T05:53:40.137",
        customerBirthday: "1999/1/1 0:00:00",
        customerGender: "1",
        customerId: 10053,
        customerMobile: "13211112222",
        customerName: "高更",
        description: '正是买健康源的时机',
        scheduledTime: "2018-09-03T05:53:00",
        state: 1,
        taskId: 10021,
        title: "让高更体检",
        type: 3
      },
    ] */
  };
  @observable pending = false;
  @observable submitStatus = false;
  @observable customerId = '';
  init = flow(function* () {
    yield this.getKpi();
    yield this.getMissions();
  });
  @action
  submit = stop => {
    if (stop) {
      this.submitStatus = false;
      return;
    }
    this.submitStatus = true;
  };
  addTask = flow(function* (value) {
    try {
      this.pending = true;
      const res = yield createTask(value);
      this.pending = false;
      return res;
    } finally {
      this.pending = false;
    }
  });
  getKpi = flow(function* () {
    const kpi = yield getKpi();
    if (kpi) {
      this.kpi = kpi;
      return kpi;
    }
  });
  getMissions = flow(function* () {
    const list = yield getTasks({ state: 1 });
    if (list) {
      this.missions = list;
    }
  });
  setTaskDone = flow(function* (id) {
    const res = yield doneTask(id);
    if (res) {
      yield this.getMissions();
    }
  });
  searchCustomer = flow(function* (key) {
    const result = yield getCustomers({ key });
    if (result) {
      return result.customers;
    }
  });
}

export default new Home();
